package com.hotel.po.mapper;

import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class RoomMapper {

    public List<Map<String, String>> findAll(Long hotelId) {
        String base = "SELECT id, hotel_id AS hotelId, room_type AS roomType, price, inventory, image_url AS imageUrl, description, created_at AS createdAt FROM room";
        return hotelId == null
                ? DBTools.queryWithBusiness(base + " ORDER BY id DESC")
                : DBTools.queryWithBusiness(base + " WHERE hotel_id=? ORDER BY id DESC", hotelId);
    }
    
    public List<Map<String, String>> search(String keyword) {
        String sql = "SELECT id, hotel_id AS hotelId, room_type AS roomType, price, inventory, image_url AS imageUrl, description, created_at AS createdAt FROM room WHERE room_type LIKE ? OR description LIKE ? ORDER BY id DESC";
        String searchPattern = "%" + keyword + "%";
        return DBTools.queryWithBusiness(sql, searchPattern, searchPattern);
    }

    public Long insert(Map<String, Object> body) {
        String sql = "INSERT INTO room(hotel_id,room_type,price,inventory,image_url,description) VALUES(?,?,?,?,?,?)";
        Long hotelId = Long.parseLong(body.get("hotelId").toString());
        String roomType = (String) body.getOrDefault("roomType", "");
        Double price = Double.parseDouble(body.get("price").toString());
        Integer inventory = Integer.parseInt(body.get("inventory").toString());
        String imageUrl = (String) body.getOrDefault("imageUrl", null);
        String description = (String) body.getOrDefault("description", null);
        DBTools.updateWithBusiness(sql, hotelId, roomType, price, inventory, imageUrl, description);
        String id = DBTools.querySingle("SELECT LAST_INSERT_ID() AS id");
        return Long.valueOf(id);
    }

    public int update(Map<String, Object> body) {
        String sql = "UPDATE room SET hotel_id=?, room_type=?, price=?, inventory=?, image_url=?, description=? WHERE id=?";
        Long id = Long.parseLong(body.get("id").toString());
        Long hotelId = Long.parseLong(body.get("hotelId").toString());
        String roomType = (String) body.getOrDefault("roomType", "");
        Double price = Double.parseDouble(body.get("price").toString());
        Integer inventory = Integer.parseInt(body.get("inventory").toString());
        String imageUrl = (String) body.getOrDefault("imageUrl", null);
        String description = (String) body.getOrDefault("description", null);
        return DBTools.updateWithBusiness(sql, hotelId, roomType, price, inventory, imageUrl, description, id);
    }

    public int delete(Long id) {
        return DBTools.updateWithBusiness("DELETE FROM room WHERE id=?", id);
    }

    public Map<String, String> findById(Long id) {
        String sql = "SELECT id, hotel_id AS hotelId, room_type AS roomType, price, inventory, image_url AS imageUrl, description, created_at AS createdAt FROM room WHERE id=?";
        java.util.List<java.util.Map<String, String>> list = DBTools.queryWithBusiness(sql, id);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    public Double calcSeasonPrice(Long roomId, String targetDate) {
        // 命中季节价则返回季节价，否则返回基础价
        String sp = DBTools.querySingle("SELECT price FROM room_price_season WHERE room_id=" + roomId + " AND '" + targetDate + "' BETWEEN start_date AND end_date ORDER BY id DESC LIMIT 1");
        if (sp != null && sp.length() > 0) {
            return Double.parseDouble(sp);
        }
        String base = DBTools.querySingle("SELECT price FROM room WHERE id=" + roomId);
        return base == null ? null : Double.parseDouble(base);
    }
}


